+function ($) {
    'use strict';
     
    kui.setRegional('progressmsg', '正在努力加载数据，请稍等...')
    
    kui.setRegional('maskBackground', '<div class="bjui-maskBackground bjui-ajax-mask"></div>')  //kk
    
    kui.setRegional('maskProgress', '<div class="bjui-maskProgress bjui-ajax-mask"><i class="fa fa-cog fa-spin"></i>&nbsp;&nbsp;#msg#<div class="progressBg"><div class="progress"></div></div></div>')  //kk
   
    
	kui.dom = function( option, element ) {  
		kui.dom.base.constructor.call(this,option, element); 
	};

     kui.dom.DEFAULTS =   { 
         onLoad: null,
		 onBeforeLoad: null,
		 onAfterLoad: null,
		 onInitLayout: null
	 }
	 
	$.widget("dom",kui.UIComponent, {   
		getType: function ()
		{
			return 'kui.dom';
		},
		dominit:function (){ 
		    var $this =this;
             $.when($this.domuiinit()).done(function(){
				 $this.domafterinit(); 
			 })
		 },
		domuiinit:function (){ 
		    var $this =this;
			var $box    = $(document)
			$.when($this.dombeforeinit()).done(function(){
				$this.trigger('load');  
				$box.trigger(kui.eventType.initDomUI);
				$box.find('> .bjui-maskProgress').find('.progress').stop().animate({width:'95%'}, 'fast')
			})
		 },
		 dombeforeinit:function (){ 
		     var $box    = $(document);  
		     this.trigger('beforeLoad');  
		     $box.trigger(kui.eventType.beforeInitDomUI);  
			 $box.find('> .bjui-maskProgress').find('.progress').stop().animate({width:'85%'}, 'fast');  
			 
		 },
		  domafterinit:function (){ 
		        var $box    = $(document);
				this.trigger('afterLoad');  
				$box.trigger(kui.eventType.afterInitDomUI); 
				//progress
				$box.find('.bjui-maskProgress').find('.progress').stop().animate({width:'100%'}, 'fast', function() {
					$box.find('.bjui-ajax-mask').fadeOut('normal', function() { $(this).remove() })
				})
		       
		 },
		 init: function() {  
		     var $this =this;
		     $(window).resize(function() {
                var ww = $(this).width();  
                setTimeout(function() {
                	$this.trigger('initLayout', [ww]); 
					$(document).trigger(kui.eventType.initLayout);
				}, 30);
				
            });
            
            setTimeout(function() {
                var ww = $(window).width()  
                $this.trigger('initLayout', [ww]);  
			    $this.dominit();
            }, 10);
		 } 
	});  
	  
     var bjui_ajaxStatus = function($target) {
			var $this    = $target
			var $offset  = $this
			var position = $this.css('position')
			
			if (position == 'static') $offset  = $this.offsetParent()
			
			var zIndex   = parseInt($offset.css('zIndex')) || 0
			var $ajaxBackground = $this.find('> .bjui-maskBackground')
			var $ajaxProgress   = $this.find('> .bjui-maskProgress')
				
			if (!$ajaxBackground.length) {
				$ajaxBackground = $(kui.regional.maskBackground)
				$ajaxProgress   = $(kui.regional.maskProgress.replace('#msg#', kui.regional.progressmsg))
				$this.prepend($ajaxBackground).prepend($ajaxProgress)
			}
			
			var bgZindex = parseInt($ajaxBackground.css('zIndex')) || 0
			var prZindex = parseInt($ajaxProgress.css('zIndex')) || 0
			
			$ajaxBackground.css('zIndex', zIndex + 1);
			$ajaxProgress.css('zIndex', zIndex + 2);
			return {$bg:$ajaxBackground, $pr:$ajaxProgress}
		};
		kui.bjui_ajaxStatus =bjui_ajaxStatus;
		
		$(document).on(kui.eventType.ajaxStatus, function(e) {
		        var ajaxMask = bjui_ajaxStatus($(e.target));
		         ajaxMask.$bg.fadeIn()
		         ajaxMask.$pr.fadeIn() 
		         ajaxMask.$pr.find('.progress').animate({width:'10%'}, 'fast')
		       })
		     .on(kui.eventType.ajaxStart, function(e, timeout, callback) {
		         var ajaxMask = bjui_ajaxStatus($(e.target));
		         ajaxMask.$bg.fadeIn();
		         ajaxMask.$pr.fadeIn();
		         ajaxMask.$pr.find('.progress').animate({width:'80%'}, timeout || 500)
		         
		         if (callback) {
		             setTimeout(function() {
		                 callback.toFunc().call(this)
		             }, 25)
		         }
		     })
		     .on(kui.eventType.ajaxStop, function(e) {
		         var ajaxMask = bjui_ajaxStatus($(e.target)) 
		         ajaxMask.$pr.find('.progress').animate({width:'100%'}, 'fast', function() {
		             ajaxMask.$bg.remove()
		             ajaxMask.$pr.remove()
		         })
		     })
		     .on(kui.eventType.ajaxError, function(e) {
		         var ajaxMask = bjui_ajaxStatus($(e.target)) 
		         ajaxMask.$bg.remove();
		         ajaxMask.$pr.remove();
		     });
     
}(jQuery);